package controllers

import (
	"cangku/models"
	beego "github.com/beego/beego/v2/server/web"
)

type CangkuController struct {
	beego.Controller
}

type Res struct {
	Code    int         `json:"code"`
	Data    interface{} `json:"data"`
	Message string      `json:"message"`
}

func (c *CangkuController) Add() {
	types := c.GetString("types")
	name := c.GetString("name")
	address := c.GetString("address")

	if types == "" || name == "" || address == "" {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "内容不能为空",
		}
		c.ServeJSON()
		return
	}

	cangku := models.Cangku{
		Types:   types,
		Name:    name,
		Address: address,
	}

	if err := models.DB.Create(&cangku).Error; err != nil {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "添加失败",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code:    200,
		Data:    cangku,
		Message: "添加成功",
	}
	c.ServeJSON()
	return
}
